package com.db.nio;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

public class NetworkCopyNIOClient extends AbstractCopy {
    public static void main(String[] args) {
        System.out.println("client started");

        ByteBuffer byteBuffer = ByteBuffer.allocate(BYTE_BUFFER_SIZE);

        try (SocketChannel socketChannel = SocketChannel.open();
             FileChannel fileChannel = new FileInputStream(FILE_1).getChannel()) {

            socketChannel.connect(new InetSocketAddress(SERVER_HOST, SERVER_PORT));
            System.out.println("connection started");

            while (fileChannel.read(byteBuffer) != -1) {
                byteBuffer.flip();
                socketChannel.write(byteBuffer);
                byteBuffer.clear();
            }

            System.out.println("success");

        } catch (IOException e) {
            System.out.println("input output exception");
        }
    }
}
